﻿using AbpOne.Domain.Blog;
using AbpOne.Domain.HotNews;
using AbpOne.Domain.Shared;
using AbpOne.Domain.Wallpaper;
using Microsoft.EntityFrameworkCore;
using Volo.Abp;

namespace AbpOne.EntityFrameworkCore {
    public static class AbpOneDbContextModelCreatingExtensions {
        public static void Configure(this ModelBuilder builder) {
            Check.NotNull(builder, nameof(builder));

            // 配置表模型，包括表名、字段类型和长度等信息
            // 此处使用的是代码优先的ORM模式
            // 微软的自定义 Code First 约定：https://docs.microsoft.com/zh-cn/ef/ef6/modeling/code-first/conventions/custom

            builder.Entity<Post>(b => {
                b.ToTable(AbpOneConst.DbTablePrefix + AbpOneDbConsts.DbTableName.Posts);
                b.HasKey(x => x.Id);
                b.Property(x => x.Title).HasMaxLength(200).IsRequired();
                b.Property(x => x.Author).HasMaxLength(10);
                b.Property(x => x.Url).HasMaxLength(100).IsRequired();
                b.Property(x => x.Html).HasColumnType("longtext").IsRequired();
                b.Property(x => x.Markdown).HasColumnType("longtext").IsRequired();
                b.Property(x => x.CategoryId).HasColumnType("int");
                b.Property(x => x.CreationTime).HasColumnType("datetime");
            });

            builder.Entity<Category>(b => {
                b.ToTable(AbpOneConst.DbTablePrefix + AbpOneDbConsts.DbTableName.Categories);
                b.HasKey(x => x.Id);
                b.Property(x => x.CategoryName).HasMaxLength(50).IsRequired();
                b.Property(x => x.DisplayName).HasMaxLength(50).IsRequired();
            });

            builder.Entity<Tag>(b => {
                b.ToTable(AbpOneConst.DbTablePrefix + AbpOneDbConsts.DbTableName.Tags);
                b.HasKey(x => x.Id);
                b.Property(x => x.TagName).HasMaxLength(50).IsRequired();
                b.Property(x => x.DisplayName).HasMaxLength(50).IsRequired();
            });

            builder.Entity<PostTag>(b => {
                b.ToTable(AbpOneConst.DbTablePrefix + AbpOneDbConsts.DbTableName.PostTags);
                b.HasKey(x => x.Id);
                b.Property(x => x.PostId).HasColumnType("int").IsRequired();
                b.Property(x => x.TagId).HasColumnType("int").IsRequired();
            });

            builder.Entity<FriendLink>(b => {
                b.ToTable(AbpOneConst.DbTablePrefix + AbpOneDbConsts.DbTableName.Friendlinks);
                b.HasKey(x => x.Id);
                b.Property(x => x.Title).HasMaxLength(20).IsRequired();
                b.Property(x => x.LinkUrl).HasMaxLength(100).IsRequired();
            });

            builder.Entity<Wallpaper>(b => {
                b.ToTable(AbpOneConst.DbTablePrefix + AbpOneDbConsts.DbTableName.Wallpapers);
                b.HasKey(x => x.Id);
                b.Property(x => x.Id).ValueGeneratedOnAdd();
                b.Property(x => x.Url).HasMaxLength(200).IsRequired();
                b.Property(x => x.Title).HasMaxLength(100).IsRequired();
                b.Property(x => x.Type).HasColumnType("int").IsRequired();
                b.Property(x => x.CreateTime).HasColumnType("datetime").IsRequired();
            });

            builder.Entity<HotNews>(b => {
                b.ToTable(AbpOneConst.DbTablePrefix + AbpOneDbConsts.DbTableName.HotNews);
                b.HasKey(x => x.Id);
                b.Property(x => x.Id).ValueGeneratedOnAdd();
                b.Property(x => x.Title).HasMaxLength(200).IsRequired();
                b.Property(x => x.Url).HasMaxLength(250).IsRequired();
                b.Property(x => x.SourceId).HasColumnType("int").IsRequired();
                b.Property(x => x.CreateTime).HasColumnType("datetime").IsRequired();
            });
        }
    }
}